#!/bin/sh
#
# Copyright 2008 Mattias Norrby
#
# This file is part of Test Dept..
#
# Test Dept. is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# Test Dept. is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with Test Dept..  If not, see <http://www.gnu.org/licenses/>.

POSSIBLE_STUBS="${1:-}"
INVOCATION="`basename ${0}`"

usage() {
    RETVAL="$1"
    echo "usage: $INVOCATION [stubs prototype header file]"
    echo "      ($INVOCATION then reads nm(1) output on stdin)"
    exit $RETVAL
}

err() {
    MSG="$1"
    echo "$INVOCATION: $MSG" >&2
    usage 2
}

case "$POSSIBLE_STUBS" in
    -h|--help)
	usage 0
	;;
esac

if [ "$POSSIBLE_STUBS" != "" ]; then
  [ -f "$POSSIBLE_STUBS" ] || err "no readable file named \"$POSSIBLE_STUBS\""
fi

sed -n '/test_dept.*set/s/.*test_dept_\(.*\)_set$/\1/p' | while read symbol; do
  sed -n "/[^A-Za-z_*0-9]${symbol}[ ]*(/p; /^#include/p; " $POSSIBLE_STUBS
done
